/*
 * Copyright (C) 2023 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.templateproject.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.umeng.analytics.pro.d;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import kotlin.Metadata;
import kotlin.jvm.JvmStatic;
//import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;

public final class NetworkUtil {
    @NotNull
    public static final Companion Companion = new Companion();
    private static int NET_CNNT_BAIDU_OK = 1;
    private static int NET_CNNT_BAIDU_TIMEOUT = 2;
    private static int NET_NOT_PREPARE = 3;
    private static int NET_ERROR = 4;
    private static final int TIMEOUT = 3000;

    /* compiled from: NetworkUtil.kt */
//    @Metadata(d1 = {"\u0000(\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u000f\n\u0002\u0010\u000e\n\u0000\n\u0002\u0010\u000b\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0086\u0003\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\b\u0010\u0013\u001a\u00020\u0014H\u0007J\u0010\u0010\u0015\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u0018H\u0007J\u0010\u0010\u0019\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u0018H\u0007J\u0010\u0010\u001a\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u0018H\u0007J\u0010\u0010\u001b\u001a\u00020\u00162\u0006\u0010\u0017\u001a\u00020\u0018H\u0007J\b\u0010\u001c\u001a\u00020\u0016H\u0003R\u001a\u0010\u0003\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0005\u0010\u0006\"\u0004\b\u0007\u0010\bR\u001a\u0010\t\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\n\u0010\u0006\"\u0004\b\u000b\u0010\bR\u001a\u0010\f\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\r\u0010\u0006\"\u0004\b\u000e\u0010\bR\u001a\u0010\u000f\u001a\u00020\u0004X\u0086\u000e¢\u0006\u000e\n\u0000\u001a\u0004\b\u0010\u0010\u0006\"\u0004\b\u0011\u0010\bR\u000e\u0010\u0012\u001a\u00020\u0004X\u0082D¢\u0006\u0002\n\u0000¨\u0006\u001d"}, d2 = {"Lcom/jcds/common/utils/NetworkUtil$Companion;", "", "()V", "NET_CNNT_BAIDU_OK", "", "getNET_CNNT_BAIDU_OK", "()I", "setNET_CNNT_BAIDU_OK", "(I)V", "NET_CNNT_BAIDU_TIMEOUT", "getNET_CNNT_BAIDU_TIMEOUT", "setNET_CNNT_BAIDU_TIMEOUT", "NET_ERROR", "getNET_ERROR", "setNET_ERROR", "NET_NOT_PREPARE", "getNET_NOT_PREPARE", "setNET_NOT_PREPARE", "TIMEOUT", "getLocalIpAddress", "", "is2G", "", d.R, "Landroid/content/Context;", "is3G", "isNetworkAvailable", "isWifi", "pingNetWork", "common_release"}, k = 1, mv = {1, 7, 1}, xi = 48)
    /* loaded from: classes.dex */
    public static final class Companion {
        private Companion() {
        }
//
//        public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
//            this();
//        }

        /* JADX INFO: Access modifiers changed from: private */
        @JvmStatic
        public final boolean pingNetWork() {
            HttpURLConnection httpURLConnection;
            Throwable th;
            HttpURLConnection httpURLConnection2 = null;
            try {
                URLConnection openConnection = new URL("http://www.baidu.com").openConnection();
                Intrinsics.checkNotNull(openConnection, "null cannot be cast to non-null type java.net.HttpURLConnection");
                httpURLConnection = (HttpURLConnection) openConnection;
                try {
                    httpURLConnection.setConnectTimeout(NetworkUtil.TIMEOUT);
                    httpURLConnection.connect();
                    httpURLConnection.disconnect();
                    return true;
                } catch (IOException unused) {
                    httpURLConnection2 = httpURLConnection;
                    if (httpURLConnection2 != null) {
                        httpURLConnection2.disconnect();
                    }
                    return false;
                } catch (Throwable th2) {
                    th = th2;
                    if (httpURLConnection != null) {
                        httpURLConnection.disconnect();
                    }
                    throw th;
                }
            } catch (IOException unused2) {
            } catch (Throwable th3) {
                th = th3;
                httpURLConnection = null;
            }
            return false;
        }

        @JvmStatic
        @NotNull
        public final String getLocalIpAddress() {
            String str = "";
            try {
                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
                while (networkInterfaces.hasMoreElements()) {
                    Enumeration<InetAddress> inetAddresses = networkInterfaces.nextElement().getInetAddresses();
                    while (inetAddresses.hasMoreElements()) {
                        InetAddress nextElement = inetAddresses.nextElement();
                        if (!nextElement.isLoopbackAddress()) {
                            str = nextElement.getHostAddress().toString();
                        }
                    }
                }
            } catch (SocketException e2) {
                e2.printStackTrace();
            }
            return str;
        }

        public final int getNET_CNNT_BAIDU_OK() {
            return NetworkUtil.NET_CNNT_BAIDU_OK;
        }

        public final int getNET_CNNT_BAIDU_TIMEOUT() {
            return NetworkUtil.NET_CNNT_BAIDU_TIMEOUT;
        }

        public final int getNET_ERROR() {
            return NetworkUtil.NET_ERROR;
        }

        public final int getNET_NOT_PREPARE() {
            return NetworkUtil.NET_NOT_PREPARE;
        }

        @JvmStatic
        public final boolean is2G(@NotNull Context context) {
            Intrinsics.checkNotNullParameter(context, "context");
            Object systemService = context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Intrinsics.checkNotNull(systemService, "null cannot be cast to non-null type android.net.ConnectivityManager");
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) systemService).getActiveNetworkInfo();
            return activeNetworkInfo != null && (activeNetworkInfo.getSubtype() == 2 || activeNetworkInfo.getSubtype() == 1 || activeNetworkInfo.getSubtype() == 4);
        }

        @JvmStatic
        public final boolean is3G(@NotNull Context context) {
            Intrinsics.checkNotNullParameter(context, "context");
            Object systemService = context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Intrinsics.checkNotNull(systemService, "null cannot be cast to non-null type android.net.ConnectivityManager");
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) systemService).getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.getType() == 0;
        }

        @JvmStatic
        public final boolean isNetworkAvailable(@NotNull Context context) {
            Intrinsics.checkNotNullParameter(context, "context");
            Object systemService = context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
            Intrinsics.checkNotNull(systemService, "null cannot be cast to non-null type android.net.ConnectivityManager");
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) systemService).getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isAvailable();
        }

        @JvmStatic
        public final boolean isWifi(@NotNull Context context) {
            Intrinsics.checkNotNullParameter(context, "context");
            Object systemService = context.getSystemService(Context.CONNECTIVITY_SERVICE);
            Intrinsics.checkNotNull(systemService, "null cannot be cast to non-null type android.net.ConnectivityManager");
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) systemService).getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.getType() == 1;
        }

        public final void setNET_CNNT_BAIDU_OK(int i2) {
            NetworkUtil.NET_CNNT_BAIDU_OK = i2;
        }

        public final void setNET_CNNT_BAIDU_TIMEOUT(int i2) {
            NetworkUtil.NET_CNNT_BAIDU_TIMEOUT = i2;
        }

        public final void setNET_ERROR(int i2) {
            NetworkUtil.NET_ERROR = i2;
        }

        public final void setNET_NOT_PREPARE(int i2) {
            NetworkUtil.NET_NOT_PREPARE = i2;
        }
    }

    @JvmStatic
    @NotNull
    public static final String getLocalIpAddress() {
        return Companion.getLocalIpAddress();
    }

    @JvmStatic
    public static final boolean is2G(@NotNull Context context) {
        return Companion.is2G(context);
    }

    @JvmStatic
    public static final boolean is3G(@NotNull Context context) {
        return Companion.is3G(context);
    }

    @JvmStatic
    public static final boolean isNetworkAvailable(@NotNull Context context) {
        return Companion.isNetworkAvailable(context);
    }

    @JvmStatic
    public static final boolean isWifi(@NotNull Context context) {
        return Companion.isWifi(context);
    }

    @JvmStatic
    private static final boolean pingNetWork() {
        return Companion.pingNetWork();
    }
}